package com.ld.security.server.user.modules.ups.service.impl;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hankcs.hanlp.HanLP;
import com.ld.security.common.core.util.ListUtil;
import com.ld.security.common.mybatis.base.service.*;
import com.ld.security.common.mybatis.constant.SqlCondition;
import com.ld.security.common.mybatis.core.wrapper.EntityWrapper;
import com.ld.security.server.user.modules.ups.constant.UpsActionConstant;
import com.ld.security.server.user.modules.ups.constant.UpsKeywordConstant;
import com.ld.security.server.user.modules.ups.entity.UpsActionEntity;
import com.ld.security.server.user.modules.ups.mapping.UpsActionMapping;
import com.ld.security.server.user.modules.ups.service.IUpsKeywordService;

import com.ld.security.server.user.modules.ups.entity.UpsKeywordEntity;

import com.ld.security.server.user.modules.ups.service.IUpsKeywordService;

import com.ld.security.server.user.modules.ups.vo.KeywordDetailsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class UpsKeywordServiceImpl  extends ISuperServiceImpl<UpsKeywordEntity>
implements
        IUpsKeywordService {

    @Autowired
    UpsActionMapping upsActionMapping;

    @Override
    public KeywordDetailsVo getDetailsByUpsUserId(String upsUserId) {
        KeywordDetailsVo e = new KeywordDetailsVo();
        e.setUpsUserId(upsUserId);
        this.proxyQuery(e);
        return e;
    }

    /**
     * 获取一周内的数据分析
     * @param upsUserId
     * @return
     */
    private List<UpsActionEntity> getActionList(String upsUserId){
        Date endTime = new Date();
        Date startTime =  DateUtil.offsetWeek(endTime,-1);


        QueryWrapper ew = EntityWrapper.of()
                .addField("upsUserId",SqlCondition.eq,upsUserId)
                .addField("crtTime",SqlCondition.between,Arrays.asList(startTime,endTime))
                .addField("status",SqlCondition.eq, UpsActionConstant.status.not_record.getValue()
                );
        return upsActionMapping.selectList(ew);

        }

    /**
     * 权重变化率
     */
    private double wtType = 0.8;

        @Override
        public void buildKeywordByUpsUserId(String upsUserId) {
            KeywordDetailsVo vo =   this.getDetailsByUpsUserId(upsUserId);
            List<UpsActionEntity> actionEntities =  this.getActionList(upsUserId);
            vo.changeWt(wtType);
            for (UpsActionEntity upsActionEntity : actionEntities) {
                    //标题关键字
                    List<String> titleList = HanLP.extractKeyword(upsActionEntity.getLabel(), UpsKeywordConstant.wtType.title.keyNumber);
                    //内容关键字
                    List<String> contentList = HanLP.extractKeyword(upsActionEntity.getContent(), UpsKeywordConstant.wtType.content.keyNumber);

                    //标题关键字
                      for (String s : titleList) {
                            UpsKeywordEntity p =  this.newInstance();
                            p.setKeyword(s);
                            p.setWtType(UpsKeywordConstant.wtType.title);
                            p.setUpsUserId(upsUserId);
                            vo.addKeyword(p);
                      }

                      //内容关键字
                      for (String s : contentList) {
                          UpsKeywordEntity p =  this.newInstance();
                          p.setKeyword(s);
                          p.setWtType(UpsKeywordConstant.wtType.content);
                          p.setUpsUserId(upsUserId);
                          vo.addKeyword(p);
                      }

                      //内容作者
                      UpsKeywordEntity author=    this.newInstance();
                      author.setKeyword(upsActionEntity.getContentCrtUser());
                      author.setWtType(UpsKeywordConstant.wtType.content_crt_user);
                      author.setUpsUserId(upsUserId);
                      vo.addKeyword(author);

                      //内容类型
                UpsKeywordEntity contentType=    this.newInstance();
                contentType.setKeyword(upsActionEntity.getContentType());
                contentType.setWtType(UpsKeywordConstant.wtType.content_type);
                contentType.setUpsUserId(upsUserId);
                vo.addKeyword(contentType);

            }



            this.proxySave(vo);



            UpsActionEntity val = new UpsActionEntity();
            val.setStatus(UpsActionConstant.status.suc_record);

            List<String> ids =  actionEntities.stream().map(v -> v.getId()).collect(Collectors.toList());

            UpdateWrapper up = new UpdateWrapper<>().in("in",ids);

            upsActionMapping.update(val,up);
       }
}
